<?php
//处理数据，转换为json数据
function response($code, $message = '', $data = array()) {
    if(!(is_numeric($code))) {
        return '';
    }
    $result = array(
        'code' => $code,
        'message' => $message,
        'data' => $data
    );
    echo json_encode($result);
    exit;
}
//执行url
function responseURL($url){
    $ch = curl_init( );
    curl_setopt( $ch,CURLOPT_URL,$url );
    curl_setopt( $ch,CURLOPT_HEADER,0 );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER,1 );
    //$ret= curl_exec( $ch );
    curl_exec( $ch );
    curl_close( $ch );
    //echo $ret;
}
function mcitUrl($id,$type){
    switch ($type) {
        case 'access':
            $url = U('auth/access',array('role'=>url_base64_encode($id)));
            break;
        case 'edit_user':
            $url = U('auth/editUser',array('id'=>url_base64_encode($id)));
            break;
        case 'access1':
            $url = U('Fac/access',array('role'=>url_base64_encode($id)));
            break;
        default:
            $url = '#404';
            break;
    }
    return $url;
}

function getParentRegion($value){
    $map1['code'] = $value;
    $pidinfo = M('new_region')->where($map1)->find();
    $map2['code'] = $pidinfo['pcode'];
    $result = M('new_region')->where($map2)->find();
    $return['data'] = $result['fullname'];
    return $return;
}

function getMajor($major){
    
}

function degree($num){
    switch ($num) {
        case '01':
        case '03':
        case '11':
        case '13':
            $str = 1;
            break;
        case '31':
        case '33':
            $str = 2;
            break;
        case '41':
        case '43':
            $str = 3;
            break;            
        default:
            $str = 0;
            break;
    }
    return $str;
}
function pwdHash($password, $type = 'sha256')
{
    return hash($type, $password);
}
function getFirstLetter($str){
    $fchar = ord($str{0});
    if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($str{0});
    $s1 = iconv("UTF-8","gb2312", $str);
    $s2 = iconv("gb2312","UTF-8", $s1);
    if($s2 == $str){$s = $s1;}
    else{$s = $str;}
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if($asc >= -20319 and $asc <= -20284) return "A";
    if($asc >= -20283 and $asc <= -19776) return "B";
    if($asc >= -19775 and $asc <= -19219) return "C";
    if($asc >= -19218 and $asc <= -18711) return "D";
    if($asc >= -18710 and $asc <= -18527) return "E";
    if($asc >= -18526 and $asc <= -18240) return "F";
    if($asc >= -18239 and $asc <= -17923) return "G";
    if($asc >= -17922 and $asc <= -17418) return "I";
    if($asc >= -17417 and $asc <= -16475) return "J";
    if($asc >= -16474 and $asc <= -16213) return "K";
    if($asc >= -16212 and $asc <= -15641) return "L";
    if($asc >= -15640 and $asc <= -15166) return "M";
    if($asc >= -15165 and $asc <= -14923) return "N";
    if($asc >= -14922 and $asc <= -14915) return "O";
    if($asc >= -14914 and $asc <= -14631) return "P";
    if($asc >= -14630 and $asc <= -14150) return "Q";
    if($asc >= -14149 and $asc <= -14091) return "R";
    if($asc >= -14090 and $asc <= -13319) return "S";
    if($asc >= -13318 and $asc <= -12839) return "T";
    if($asc >= -12838 and $asc <= -12557) return "W";
    if($asc >= -12556 and $asc <= -11848) return "X";
    if($asc >= -11847 and $asc <= -11056) return "Y";
    if($asc >= -11055 and $asc <= -10247) return "Z";
    return null;
}

function pinyin($zh,$num){
    $ret = "";
    $s1 = iconv("UTF-8","gb2312", $zh);
    $s2 = iconv("gb2312","UTF-8", $s1);
    if($s2 == $zh){$zh = $s1;}
    for($i = 0; $i < strlen($zh); $i++){
        $s1 = substr($zh,$i,1);
        $p = ord($s1);
        if($p > 160){
            $s2 = substr($zh,$i++,2);
            $ret .= getFirstLetter($s2);
        }else{
            $ret .= $s1;
        }
    }
    $ret=substr($ret,0,$num);
    return $ret;
}

function getInternshipDays($end,$time)
{
    if($end=="")  // 未结束
    {
        return floor((time()-strtotime($time))/(3600*24));
    }
    else
    {
        return floor((strtotime($end)-strtotime($time))/(3600*24));
    }
}


function convertTime($data,$time,$type)
{
    for($a=0;$a<count($data);$a++)
    {
        $data[$a][$time]=date($type,$data[$a][$time]);
    }
    return $data;
}
function contentShort($data)  // 正文转化成摘要
{
    for($a=0;$a<count($data);$a++)
    {
        $length=strlen($data[$a]['content']);
        $data[$a]['content']=substr($data[$a]['content'],0,48);
        if($length>=48) {$data[$a]['content'].="..";}
    }
    return $data;
}


function getRegion($account)
{
    $mid = M('user')->where("account = '".$account."'")->getField('id');
    return M('mzuser')->where("uid='".$mid."'")->getField('region_code');
}

function getDaysbyMonth($i)
{
    switch ($i) {
        case '1':
            return 31;
            break;
        case '2':
            return 30;
            break;
        case '3':
            return 31;
            break;
        case '4':
            return 30;
            break;
        case '5':
            return 31;
            break;
        case '6':
            return 30;
            break;
        case '7':
            return 31;
            break;
        case '8':
            return 31;
            break;
        case '9':
            return 30;
            break;
        case '10':
            return 31;
            break;
        case '11':
            return 30;
            break;
        case '12':
            return 31;
            break;

        default:

            break;
    }
}

/**
 * 递归重新排序无限级分类数组
 */
function recursive ($array, $pid=0, $level=0)
{
	$arr = array();
	foreach ($array as $v)
    {
		if ($v['pid'] == $pid)
        {
			$v['level'] = $level;
			$v['html'] = str_repeat('--', $level);
			$arr[] = $v;
			$arr = array_merge($arr, recursive($array, $v['id'], $level + 1));
		}
	}

	return $arr;
}
/**
 * 获取传递ID查出所有子分类ID
 * @param  [type] $array [description]
 * @param  [type] $id    [description]
 * @return [type]        [description]
 */
function get_all_child ($array, $id)
{
	$arr = array();
	foreach ($array as $v)
    {
		if ($v['pid'] == $id) 
        {
			$arr[] = $v['id'];
			$arr = array_merge($arr, get_all_child($array, $v['id']));
		}
	}
	return $arr;
}
/**
 * 邮件发送函数
 */
function sendMail($to, $title, $content,$xls='') {

    Vendor('PHPMailer.PHPMailerAutoload');
    $mail = new PHPMailer(); //实例化
    $mail->IsSMTP(); // 启用SMTP
    $mail->Host=C('MAIL_HOST'); //smtp服务器的名称
    $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
    $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
    $mail->Password = C('MAIL_PASSWORD') ; //邮箱密码
    $mail->From = C('MAIL_FROM'); //发件人地址（也就是你的邮箱地址）
    $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
    $mail->AddAddress($to,"尊敬的客户");
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
    $mail->CharSet=C('MAIL_CHARSET'); //设置邮件编码
    $mail->Subject =$title; //邮件主题
    $mail->Body = $content; //邮件内容
    $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
    $mail->AddAttachment($xls); // 添加附件,并指定名称
    return($mail->Send());
}

